package hw.txtreader.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import hw.txtreader.R;
import hw.txtreader.activity.LoginActivity;
import hw.txtreader.activity.RegisterActivity;
import hw.txtreader.base.BaseFragment;
import hw.txtreader.databinding.FragmentForthBinding;
import hw.txtreader.utils.LogUtil;
import hw.txtreader.utils.SPUtil;

/**
 * 是否登录： module_is_logon  是 1  否 0
 * 用户账号： module_user_account
 * 用户密码： module_user_password
 * 用户昵称： module_user_username
 *
 */
public class ForthFragment extends BaseFragment {


    List<String> list = new ArrayList<>();
    FragmentForthBinding binding;
    String username,account;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_forth, null, false);
        binding.setFragment(this);

        initView();
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();

        Type listType = new TypeToken<List<String>>() {
        }.getType();
        String spRes = SPUtil.getString(getActivity(),"module_list_user", "");
        LogUtil.e("spRes-->"+ spRes);
        if (spRes != null) {
            if (!TextUtils.isEmpty(spRes)) {
                Gson gson = new Gson();
                list = gson.fromJson(spRes, listType);
            }
        }
        account = SPUtil.getString(getActivity(),"module_user_account", "");
        if (TextUtils.isEmpty(account)) {
            binding.llModuleUnLogin.setVisibility(View.VISIBLE);
            binding.tvModuleOut.setVisibility(View.GONE);
            binding.tvModuleName.setVisibility(View.GONE);
        } else {

            for(String str : list){
                LogUtil.e("1-->"+ str.split(" ")[1] + " 2-->"+ account);
                if (str.split(" ")[1].equals(account)) {
                    binding.tvModuleName.setText(str.split(" ")[0]);
                    SPUtil.putString(getActivity(),"module_user_username",str.split(" ")[0]);
                    LogUtil.e("str-->"+ str);
                }
            }
            binding.llModuleUnLogin.setVisibility(View.GONE);
            binding.tvModuleOut.setVisibility(View.VISIBLE);

        }
    }

    private void initView() {
        binding.tvModuleLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
        });

        binding.tvModuleRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), RegisterActivity.class));
            }
        });

        binding.tvModuleOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),LoginActivity.class));
                getActivity().finish();
            }
        });
    }

}
